473,432 Members | 1,595 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,432 software developers and data experts.

select option by value

107 100+
Hi Guys,

I've hit a wall, im trying to create a function to make an option in a <select> box selected when given its value, rather than its index. I cant seem to figure out how to loop through each value for each option.

This is what I have so far, but its not correct...
"selObj" is the <select> as an object


Expand|Select|Wrap|Line Numbers
  1. function selectOptionByValue(selObj, value){
  2.  
  3.     for (options in selObj.options){
  4.         if (options.value == value){
  5.             options.selected = true;
  6.             break;
  7.     }
  8.  
  9. }
  10.  

Any help is much apprieciated1

Thanks

Andy
May 21 '08 #1
3 22164
mrhoo
428 256MB
a select element has a property named 'options'-
use an indexed array to loop through the options.

If the select can only select 1 option, set the selectedIndex,
or you have to unset whatever was previously selected.


Expand|Select|Wrap|Line Numbers
  1. function selectOptionByValue(selObj, val){
  2.     var A= selObj.options, L= A.length;
  3.     while(L){
  4.         if (A[--L].value== val){
  5.             selObj.selectedIndex= L;
  6.             L= 0;
  7.         }
  8.     }
  9. }
May 21 '08 #2
theS70RM
107 100+
that works well,

thanks for your help!

Andy
May 22 '08 #3
acoder
16,027 Expert Mod 8TB
If you want a shortcut for single select object, just set its value property. Would work in at least Firefox/IE. mrhoo's method is obviously more robust and reliable though.
May 22 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: steve cole | last post by:
Hi All No doubt I am doing something stupid since i am new to javascript I have a problem with the following code, when run on IE6 the value for opt_sel is null but on Mozilla or konqueror the...
9
by: Gianni | last post by:
I have to insert in a html select the last 10 years <select name="year" onChange="month()" size=5> <option value="1994">1994</option> <option value="1995">1995</option> <option...
3
by: ANTISPAM_garycnew_ANTISPAM | last post by:
What is the simplest way to retain the last option value selected in an html select object using javascript? I am currently using a server-side cgi language to accomplish this task, but it adds...
11
by: 75battle | last post by:
I am allowing a user to selected a date to schedule an event and it seems like the best way to do this is with drop down menus for Year, month, date. However I would like to make it so when you...
5
by: = poster = | last post by:
Hi all , I have a script which let the user choose between four days : Day: <select name=\"day\" value=\"$day\"> <option value=\"01\">1</option> <option value=\"02\">2</option> <option...
4
by: magix | last post by:
Hi, in my SELECT (like below), the option value has 2 values, separated with comma "," <select name="sIdName"> <option value="10,John">John</option> <option value="11,Eva">Eva</option>...
5
by: usa51 | last post by:
Hi, I have a dropdown options value from 2 to 5. After I change the selection, in the same asp page, I also have a button "Add", how do I dynamically add 3 text fields if I select value 3 and then...
1
by: omar999 | last post by:
i have 2 select dropdowns and I'm trying to make an iframe src change according to the selected hotel dropdown. at the moment when you select a city the corresponding hotels are loaded up via...
10
by: Vivekneo | last post by:
Hi I am very new to web programming, trying to achieve a task, I have a form with 3 dropdown box and a text field(which is readonly). My problem now is, after selecting the above 3 dropdown boxes,...
2
by: saurabh moses | last post by:
hi can anybody help me on <select> option in website I have multiple option bar with multiple choices i want when i select one choice in one bar it show only related choices on another option bar....
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.